<?php
namespace scripts;

use globalunit\logic\Web3DLogic;
use QYS\Protocol\Response;
use QYS\Protocol\Request;
use QYS\Task\Factory;
use QYS\Db\Mysql;
use QYS\Util\Debug;
use QYS\Log\Log;
use QYS\Db\Redis;
use globalunit\utils\DBClass;
use globalunit\utils\Config;
use globalunit\utils\JWTUtil;
use globalunit\utils\Utils;
use globalunit\utils\KeysUtil;
use QYS\Core\Config as QYSConfig;
use QYS\QYS;


class Access
{
    public function access($request,$response,$params)
    {
        $response->addHeader("Content-Type", 'application/json');
        $response->addHeader("Access-Control-Allow-Origin", "*");
        $response->addHeader("Access-Control-Allow-Methods", 'POST, GET, OPTIONS, DELETE');
        $response->addHeader('Access-Control-Allow-Headers', "Origin, X-Requested-With, Content-Type, Accept");
        $response->sendHttpHeader();
        if ($request->getRequest()->server['request_method'] == 'OPTIONS') {
            $response->say("");
            $response->status(200);
            QYS::bye();
            return;
        }
        $jwt = $request->getString("jwt");
        if (!empty($jwt)) {
            $GAMENAME =  $GLOBALS['GAMENAME'];
            $VERSION =  $GLOBALS['VERSION'];
            try {
                $jwtobj = JWTUtil::decode($jwt, "$GAMENAME-$VERSION", array('HS256'));
            } catch (\Exception $e) {
                $errcode = 401;
                $response->say(json_encode(array("errcode" => $errcode, "errmsg" => '请重新登录', "Message" => $e->getMessage())));
                QYS::bye();
                return;
            }
            if (empty($jwtobj) || empty($jwtobj->id) || empty($jwtobj->token)) {
                $errcode = 1;
                $response->say(json_encode(array("errcode" => $errcode, "errmsg" => '登录验证失败')));
                QYS::bye();
                return;
            }
            $id = $jwtobj->id;
            $token = $jwtobj->token;
            $rediscli = Redis::getInstance("redis1");
            $tokenkey = KeysUtil::get_main_token($id);
            $token0 = $rediscli->get($tokenkey);
            if ($token0 != $token) {
                $errcode = 2;
                $response->say(json_encode(array("errcode" => $errcode, "errmsg" => "账户已经在其他终端上登录")));
                QYS::bye();
                return;
            }

            $appid = $jwtobj->appid;
            if (empty($appid)) {
                $errcode = 4;
                $response->say(json_encode(array("errcode" => $errcode, "errmsg" => "系统繁忙请稍等后再试")));
                QYS::bye();
                return;
            }

            $res = Web3DLogic::getWeb3dByAppid($appid);

            if (empty($res)) {
                $errcode = 401;
                $response->say(json_encode(array("errcode" => $errcode, "errmsg" => "系统繁忙请稍等后再试")));
                QYS::bye();
                return;
            }

            $myid = $res['id'];

            //活动未开始
            if(strtotime($res["start_time"]) > time()){
                $errcode = 6;
                $response->say(json_encode(array("errcode" => $errcode, "errmsg" => "活动未开始!")));
                QYS::bye();
                return;
            }

            //活动已结束
            if(strtotime($res["end_time"]) < time()){
                $errcode = 7;
                $response->say(json_encode(array("errcode" => $errcode, "errmsg" => "活动已结束!")));
                QYS::bye();
                return;
            }

            $request->getRequest()->get["id"] = $id;
            $request->getRequest()->get["wid"] = $myid;
            $request->getRequest()->get["appid"] = $appid;
        }
    }

}
